package org.example;

import java.util.Arrays;
import java.util.Stack;

public class MyStack {
    private int[] elem;
    private int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }



    //栈的压入弹出序列
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> s = new Stack<>();
        int i = 0;
        int j = 0;
        while (i < pushV.length) {
            s.push(pushV[i]);
            while (!s.isEmpty() && j < popV.length && s.peek() == popV[j]) {
                s.pop();
                j++;
            }
            i++;
        }
        return s.isEmpty();

    }


    //逆波兰表达式求值
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String s : tokens) {
            //数字入栈
            if (!isOpera(s)) {
                int val = Integer.parseInt(s);
                stack.push(val);
            }else {
                //将栈中两个元素弹出，并经过操作符计算后再次入栈
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (s) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }

    private boolean isOpera(String s) {
        if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
            return true;
        }
        return false;
    }

    //括号匹配问题
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //如果是左括号就入栈
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                //如果是右括号就匹配
                if (stack.empty()) {
                    //如果栈空了就直接返回错误
                    return false;
                }else {
                    //如果栈不空，就进行匹配
                    //先将栈顶元素取出
                    char tmp = stack.pop();
                    if (ch == ')' && tmp == '(' || ch == '}' && tmp == '{' || ch == ']' && tmp == '[') {
                        continue;
                    }else {
                        return false;
                    }
                }
            }
        }
        if (!stack.empty()) {
            return false;
        }
        return true;
    }


    //入栈
    public void push(int val) {
        // 满了要扩容
        if (isFull()) {
            resize();
        }
        this.elem[usedSize] = val;
        usedSize++;
    }


    private boolean isFull() {
        return usedSize == elem.length;
    }

    //出栈
    public void pop() {
        if (isEmpty()) {
            throw new StackEmptyException("Stack is empty");
        }
        this.elem[--usedSize] = 0;
    }
    //获取栈顶元素
    public int peek() {
        if (isEmpty()) {
            throw new StackEmptyException("Stack is empty");
        }
        return this.elem[usedSize-1];
    }

    //扩容
    private void resize() {
        elem = Arrays.copyOf(elem, usedSize * 2);
    }

    private boolean isEmpty() {
        return usedSize == 0;
    }



}
